package routers

import (
	"log"

	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
	"hass.tree.com/bridge/auth"
	"hass.tree.com/bridge/model"
	"hass.tree.com/bridge/vo"
)

func LoadCommon(e *gin.Engine) {
	commonRoute := e.Group("/common")
	{
		commonRoute.Use(auth.JWTauth())
		commonRoute.POST("/list", commonList)
		commonRoute.POST("/add", commonAdd)
		commonRoute.POST("/delete", commonDelete)
		commonRoute.POST("/update", commonUpdate)
	}

}

// 查询数据
// "collection":"TEST_COLLECTION",
// "condition":{
// 		"num":2,
// 		"$or":[
// 				{
// 						"message":{
// 								"$regex":"11"
// 						}
// 				},
// 				{
// 						"message":{
// 								"$regex":"22"
// 						}
// 				}
// 		]
// },
// "sort":["num"],
// "pageQuery":{
// 		"page":1,
// 		"size":10
// }
func commonList(c *gin.Context) {

	data, err := c.GetRawData()
	if err != nil {
		log.Fatal(err)
	}
	jsonStr := string(data)
	collection := gjson.Get(jsonStr, "collection")
	if len(collection.String()) == 0 {
		vo.Fail("collection is required", c)
		return
	}
	condition := gjson.Get(jsonStr, "condition")
	sort := gjson.Get(jsonStr, "sort")
	pageQuery := gjson.Get(jsonStr, "pageQuery")
	sorts := sort.Array()
	var sortArray []string
	for _, value := range sorts {
		sortArray = append(sortArray, value.String())
	}
	results := model.MongoList(collection.String(), condition.String(), sortArray, pageQuery.String())
	vo.Success(results, c)
}

// 新增数据
// "collection":"TEST_COLLECTION",
// "data":{
// 		"topic":"goTopic",
// 		"message":"444444",
// 		"num":4
// }
func commonAdd(c *gin.Context) {
	data, err := c.GetRawData()
	if err != nil {
		log.Fatal(err)
	}
	jsonStr := string(data)
	collection := gjson.Get(jsonStr, "collection")
	if len(collection.String()) == 0 {
		vo.Fail("collection is required", c)
		return
	}
	tableData := gjson.Get(jsonStr, "data")
	if len(tableData.String()) == 0 {
		vo.Fail("data is required", c)
		return
	}

	model.MongoAdd(collection.String(), tableData.String())
	vo.Success("", c)
}

// 删除数据
// "collection":"TEST_COLLECTION",
// "id":"62bbb04eef15413d63f42eb4"
func commonDelete(c *gin.Context) {

	data, err := c.GetRawData()
	if err != nil {
		log.Fatal(err)
	}
	jsonStr := string(data)
	collection := gjson.Get(jsonStr, "collection")
	if len(collection.String()) == 0 {
		vo.Fail("collection is required", c)
		return
	}
	id := gjson.Get(jsonStr, "id")
	if len(id.String()) == 0 {
		vo.Fail("id is required", c)
		return
	}
	model.MongoDelete(collection.String(), id.String())
	vo.Success("", c)
}

// 更新数据
// "collection":"TEST_COLLECTION",
// "data":{
// 	"id":"62bbb03fef15413d63f42eac",
// 	"params":{
// 			"message":"test1111",
// 			"num":1
// 	}
// }
func commonUpdate(c *gin.Context) {

	data, err := c.GetRawData()
	if err != nil {
		log.Fatal(err)
	}
	jsonStr := string(data)
	collection := gjson.Get(jsonStr, "collection")
	if len(collection.String()) == 0 {
		vo.Fail("collection is required", c)
		return
	}
	tableData := gjson.Get(jsonStr, "data")
	if len(tableData.String()) == 0 {
		vo.Fail("data is required", c)
		return
	}
	id := gjson.Get(tableData.String(), "id")
	if len(id.String()) == 0 {
		vo.Fail("data.id is required", c)
		return
	}

	params := gjson.Get(tableData.String(), "params")
	if len(id.String()) == 0 {
		vo.Fail("data.params is required", c)
		return
	}
	model.MongoUpdate(collection.String(), id.String(), params.String())
	vo.Success("", c)
}
